<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
request.setCharacterEncoding("UTF-8");
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String htmlData = request.getParameter("content1") != null ? request.getParameter("content1") : "";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'newproduct.jsp' starting page</title>
    <meta charset="utf-8" />
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	
	<link rel="stylesheet" href="<%=basePath%>themes/default/default.css" />
	<link rel="stylesheet" href="<%=basePath%>plugins/code/prettify.css" />
	<script charset="utf-8" src="<%=basePath%>kindeditor.js"></script>
	<script charset="utf-8" src="<%=basePath%>kindeditor-min.js"></script>
	<script charset="utf-8" src="<%=basePath%>lang/zh_CN.js"></script>
	<script charset="utf-8" src="<%=basePath%>plugins/code/prettify.js"></script>
  	<script>
  				function newproduct(){
  					var newName=$("#newproduct").val();
  					var content=editor1.html();
  					var productsize=$("#productsize").val();
  					$.post("InsertProductAction",{"productName":newName,"productSize":productsize,"content":content},function(data){
  							if(data=="success"){
  								alert("增加成功");
  								editor1.html("");
  								$("#newproduct").val("");
  								$("#productsize").val("");
  							}else if(data=="fail"){
  								alert("增加失败");
  								$("#newproduct").val("");
  								$("#productsize").val("");
  							}
  					});
  				}
  				function checkproductName(){
  					var newName=$("#newproduct").val();
  					
  					$.post("checkProductNameAction",{"productName":newName},function(data){
  							if(data=="yes"){
  								alert("这个产品已经存在");
  								$("#newproduct").val("");
  								return null;
  							}else{
  								if(isNaN($("#productsize").val())){
  									alert("请输入正确的字号");
  									$("#productsize").val("");
  									return null;
  								}else{
  									newproduct();
  								}
  								
  							}
  					});
  				}
  				var editor1;
  				KindEditor.ready(function(K) {
			 editor1 = K.create('textarea[name="content1"]', {
				cssPath : '<%=basePath%>plugins/code/prettify.css',
				uploadJson : '<%=basePath%>BackstageManagement/upload_json.jsp',
				fileManagerJson : '<%=basePath%>BackstageManagement/file_manager_json.jsp',
				allowFileManager : true,
				afterCreate : function() {
					var self = this;
					K.ctrl(document, 13, function() {
						self.sync();
						document.forms['example'].submit();
					});
					K.ctrl(self.edit.doc, 13, function() {
						self.sync();
						document.forms['example'].submit();
					});
				}
			});
			prettyPrint();
		});

  	</script>
  </head>
  
  <body>
  <%=htmlData%>
    		<div align="center">
   					<h3>增加新产品</h3>
   					<label>新的产品名:</label>
   					<input type="text" name="newproduct" id="newproduct"><br/><br/>
   					<label>字&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;号:</label>
   					<input type="text" name="productsize" id="productsize"><br/><br/>
   					产品描述
   					<form name="example" method="post" action="newproduct.jsp">
   					<textarea name="content1" id="editor_id" cols="100" rows="8" style="width:700px;height:200px;visibility:hidden;"><%=htmlspecialchars(htmlData)%></textarea>
		
		<br />
		</form>
   					<input type="button" onclick="checkproductName()" value="添加">
   			</div>
  </body>
</html>
<%!
private String htmlspecialchars(String str) {
	str = str.replaceAll("&", "&amp;");
	str = str.replaceAll("<", "&lt;");
	str = str.replaceAll(">", "&gt;");
	str = str.replaceAll("\"", "&quot;");
	return str;
}
%>
